<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <link rel="icon" type="image/png" sizes="16x16" href="./rainbow.png">
    <!-- import CSS -->
    <link rel="stylesheet" href="./element-ui/lib/theme-chalk/index.css">
</head>
<body>
<div id="app">
    <el-upload
            action="/photo/upload"
            list-type="picture-card"
            :on-success="uploadSuccess"
            :on-error="uploadError"
            :auto-upload="true">
        <i slot="default" class="el-icon-plus"></i>
        <div slot="file" slot-scope="{file}">
            <img
                    class="el-upload-list__item-thumbnail"
                    :src="file.url" alt=""
            >
            <span class="el-upload-list__item-actions">
        <span
                class="el-upload-list__item-preview"
                @click="handlePictureCardPreview(file)"
        >
          <i class="el-icon-zoom-in"></i>
        </span>
        <span
                v-if="!disabled"
                class="el-upload-list__item-delete"
                @click="handleDownload(file)"
        >
          <i class="el-icon-download"></i>
        </span>
        <span
                v-if="!disabled"
                class="el-upload-list__item-delete"
                @click="handleRemove(file)"
        >
          <i class="el-icon-delete"></i>
        </span>
      </span>
        </div>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
        <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>


    <el-row>
        <el-button @click="xuanzhuan">旋转</el-button>
    </el-row>

    <div class="demo-image__placeholder" v-if="isShow">
        <div class="block">
            <span class="demonstration">转换后</span>
            <el-image :src="src"></el-image>
        </div>
        <el-row>
            <el-button @click="download">下载此图</el-button>
        </el-row>
        <span v-down="src">(附件)</span>
    </div>
</div>
</body>
<!-- import Vue before Element -->
<script src="./vue/vue.min.js"></script>
<!-- import JavaScript -->
<script src="./element-ui/lib/index.js"></script>
<script src="./axios/axios.min.js"></script>
<script>
    new Vue({
        el: '#app',
        data() {
            return {
                dialogImageUrl: '',
                dialogVisible: false,
                disabled: false,
                originalId: '',
                isShow: false,
                src: '',
                newPicId: ''
            };
        },
        methods: {
            handleRemove(file) {
                console.log(file);
            },
            handlePictureCardPreview(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            },
            handleDownload(file) {
                console.log(file);
            },
            uploadSuccess(file) {
                console.log(file);
                this.originalId = file.id
            },
            uploadError(file) {
                console.log(file)
            },
            xuanzhuan() {
                console.log(this.originalId)
                let that = this
                axios.get('/photo/' + that.originalId, {
                    'params': {
                        'operate': 'R90'
                    }
                })
                    .then(function (response) {
                        console.log(response);
                        that.isShow = true
                        that.src = response.data.url
                        that.newPicId = response.data.id
                    })
                    .catch(function (error) {
                        console.log(error);
                    });

            },
            download() {
                console.log("下载")
                let that = this
                axios.get('/photo/download?pid=' + this.newPicId, {responseType: 'blob'})
                    .then(function (response) {
                        console.log(response.data);
                        that.downloadFile(response.data)
                    })
                    .catch(function (error) {
                        console.log(error);
                    });
            },
            downloadFile(data) {
                // 文件导出
                if (!data) {
                    return
                }
                let url = window.URL.createObjectURL(new Blob([data]));
                let link = document.createElement('a');
                link.style.display = 'none';
                link.href = url;
                link.setAttribute('download', '1.jpg');
                document.body.appendChild(link);
                link.click()
            }
        }
    })
    //自定义指令
    // Vue.directive('down', {
    //     inserted: (el, binding) => {
    //         el.style.cssText = 'cursor: pointer;color:write;'
    //         el.addEventListener('click', () => {
    //             console.log(binding.value)
    //             let link = document.createElement('a')
    //             let url =  binding.value
    //             // 这里是将url转成blob地址，
    //             fetch(url).then(res => res.blob()).then(blob => { // 将链接地址字符内容转变成blob地址
    //                 link.href = URL.createObjectURL(blob)
    //                 console.log(link.href)
    //                 link.download = ''
    //                 document.body.appendChild(link)
    //                 link.click()
    //             })
    //             // link.href = url
    //             // console.log(link.href)
    //             // link.download = ''
    //             // document.body.appendChild(link)
    //             // link.click()
    //         })
    //     }
    // })

</script>
</html>